package com.gnerv.pabu.basic.core.controller;

import com.gnerv.pabu.basic.core.entity.DingDingText;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.entity.Webhook;
import com.gnerv.pabu.basic.core.service.IDingDingService;
import com.gnerv.pabu.basic.feign.client.ComputeClient;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础平台-微服务管理
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "测试用")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/test")
public class DemoTestController {

    @Resource
    private IDingDingService dingDingService;

    @Autowired
    Configuration configuration;

    @Autowired
    ComputeClient computeClient;

    @ApiOperation("dingding")
    @RequestMapping(value = "/dingding", method = RequestMethod.POST)
    public Result updateCloudServiceStatus(@RequestBody DingDingText dingDingText) {
        Webhook webhook = new Webhook();
        webhook.setKeyWord("报警: ");
        webhook.setUrl("https://oapi.dingtalk.com/robot/send?access_token=a54d36352f90cb3ee831397d74f67f8079c088eab9e36f7c784d7e3a5a21f85a");
        dingDingService.sendTextMsg(webhook, dingDingText);
        return Result.response();
    }

    @ApiOperation("freemarker")
    @RequestMapping(value = "/freemarker", method = RequestMethod.POST)
    public Result freemarker() throws IOException, TemplateException {
        //加载模板
        Template template = configuration.getTemplate("cms/index.ftl");
        //数据模型
        Map<String, Object> map = new HashMap<>();
        map.put("name", "john");
        //静态化
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        //静态化内容
        System.out.println(content);
        //输出文件


        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        File file = new File(path + "/public");
        boolean exists = file.exists();
        if(!exists){
            file.mkdir();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(new File(file, "index.html"));
        FileCopyUtils.copy(content.getBytes(), fileOutputStream);
        return Result.response();
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result add() {
        Integer add = computeClient.add(1, 1);
        return Result.response();
    }


}
